#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Boot_WriteDataById_0x2E_test.py
@Time    :   2024/09/11 13:31:28
@Author  :   yangliang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from urllib import response
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable
import Set_up

from sqlalchemy import false
sys.dont_write_bytecode = True


WriteDID_NRC = [([0x2E],                                                                                [0x7F, 0x2E, 0x13]),  # NRC13     
                ([0x2E, 0xF1],                                                                          [0x7F, 0x2E, 0x13]),
                ([0x2E, 0x12],                                                                          [0x7F, 0x2E, 0x13]),
                ([0x2E, 0x12, 0x66],                                                                    [0x7F, 0x2E, 0x13]),
                ([0x2E, 0xF1, 0x84, 0x00],                                                              [0x7F, 0x2E, 0x13]),
                ([0x2E, 0xF1, 0x84, 0x00, 0x00, 0x00, 0x22, 0x09, 0x30,0x00, 0x00, 0x00, 0x01, 0x01],   [0x7F, 0x2E, 0x13]),
                ([0x2E, 0xF1, 0x90, 0xff],                                                              [0x7F, 0x2E, 0x31]),
                ([0x2E, 0xF1, 0x84, 0x28, 0xFF, 0x01],                                                  [0x7F, 0x2E, 0x13]),
                ([0x2E, 0x12, 0x6A, 0xff, 0x15, 0x11, 0x01],                                            [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x6B, 0x28, 0xFF, 0x01],                                                  [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x6C, 0x55, 0xFF, 0x01],                                                  [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x3F, 0x87, 0x46, 0x30, 0x32, 0x2D, 0x33, 0x36, 0x30, 0x32],                    [0x7F, 0x2E, 0x31]),
                ([0x2E, 0xF1, 0x87, 0x46, 0x30, 0x32, 0x2D, 0x33, 0x36, 0x30, 0x32],                    [0x7F, 0x2E, 0x31]),
                ([0x2E, 0xF1, 0x83, 0x00, 0x00, 0x00, 0x20, 0x22, 0x09, 0x30, 0x00, 0x00, 0x00],        [0x7F, 0x2E, 0x31]),
                ([0x2E, 0xF1, 0x8A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],        [0x7F, 0x2E, 0x31]),
                ([0x2E, 0xF1, 0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],              [0x7F, 0x2E, 0x31]),
                ([0x2E, 0xF1, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],              [0x7F, 0x2E, 0x31]),
                ([0x2E, 0xF1, 0x8B, 0x00, 0x00, 0x00, 0x20, 0x22, 0x09, 0x30],                          [0x7F, 0x2E, 0x31]),
                ([0x2E, 0xF1, 0x8C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0xF1, 0x99, 0x00, 0x00, 0x00, 0x20, 0x22, 0x09, 0x30],                          [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00],                                      [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x62, 0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x63, 0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x65, 0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x69, 0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0xF1, 0x90, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfE], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x6A, 0x00, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                ([0x2E, 0x12, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31])]


class Test_WriteDataById_Phy_Boot(object):
    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.description('Test DID 0xF184 of 0x2E in programming session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_WriteDataById_F184_Programming_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x2E, 0xF1, 0x84, 0x22, 0x09, 0x30, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02], [0x7F, 0x2E, 0x33], 2, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x2E, 0xF1, 0x84, 0x22, 0x09, 0x30, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02], [0x6E, 0xF1, 0x84], 0, iStep, True, unlock_level = 3)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0xF1, 0x84], [0x62, 0xF1, 0x84, 0x22,0x09, 0x30, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02], 0, iStep, False)
        


    ids = ["Request:{0}".format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in WriteDID_NRC]
    @allure.severity('minor')
    @allure.description('Test NRC of 0x2E in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', WriteDID_NRC, ids=ids)
    def test_WriteDataById_NRC_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        if req == [0x2E, 0x12, 0x66]:
            resp = [0x7F, 0x2E, 0x31]
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)

    @allure.severity('minor')
    @allure.description('Test DID of 0x2E in programming session with physical addressing after reset')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_WriteDataById_value_after_reset_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x2E, 0xF1, 0x84, 0x22, 0x09, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], [0x6E, 0xF1, 0x84], 2, iStep, True)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0xF1, 0x84], [0x62, 0xF1, 0x84, 0x22, 0x09, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 0, iStep, False)
        
        iStep = Driver['CAN'].restartMRR_11(Driver, iStep)
        
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0xF1, 0x84], [0x62, 0xF1, 0x84, 0x22, 0x09, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 2, iStep, False)

    @allure.severity('minor')
    @allure.description('Test DID all value set to 1 of 0x2E with unlock in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_WriteDataById_all_value_set1_with_unlock_then_reset_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x2E, 0xF1, 0x84, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01], [0x6E, 0xF1, 0x84], 2, iStep, True)
        iStep = Driver['CAN'].restartMRR_Power(Driver, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0xF1, 0x84], [0x62, 0xF1, 0x84, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01], 2, iStep, False)


    @allure.severity('minor')
    @allure.description('Test DID all value set to 0 of 0x2E with unlock in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_WriteDataById_all_value_set0_with_lock_then_reset_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x2E, 0xF1, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], [0x6E, 0xF1, 0x84], 2, iStep, True)
        iStep = Driver['CAN'].restartMRR_Power(Driver, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0xF1, 0x84], [0x62, 0xF1, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 2, iStep, False)

